import datetime
import time
 
import bluetooth
 
 
class BluetoothConnection:
    def __init__(self):
        # 是否找到到设备
        self.find = False
        # 附近蓝牙设备
        self.nearby_devices = None
 
    def find_nearby_devices(self):
        print("Detecting nearby Bluetooth devices...")
        # 可传参数 duration--持续时间 lookup-name=true 显示设备名
        # 大概查询10s左右
        # 循环查找次数
        loop_num = 3
        i = 0
        try:
            self.nearby_devices = bluetooth.discover_devices(lookup_names=True, duration=5)
            while self.nearby_devices.__len__() == 0 and i < loop_num:
                self.nearby_devices = bluetooth.discover_devices(lookup_names=True, duration=5)
                if self.nearby_devices.__len__() > 0:
                    break
                i = i + 1
                time.sleep(2)
                print("No Bluetooth device around here! trying again {}...".format(str(i)))
            if not self.nearby_devices:
                print("There's no Bluetooth device around here. Program stop!")
            else:
                print("{} nearby Bluetooth device(s) has(have) been found:".format(self.nearby_devices.__len__()), self.nearby_devices)  # 附近所有可连的蓝牙设备s
        except Exception as e:
            # print(traceback.format_exc())
            # 不知是不是Windows的原因，当附近没有蓝牙设备时，bluetooth.discover_devices会报错。
            print("There's no Bluetooth device around here. Program stop(2)!")
 
    def find_target_device(self, target_name, target_address):
        self.find_nearby_devices()
        if self.nearby_devices:
            for addr, name in self.nearby_devices:
                if target_name == name and target_address == addr:
                    print("Found target bluetooth device with address:{} name:{}".format(target_address, target_name))
                    self.find = True
                    break
            if not self.find:
                print("could not find target bluetooth device nearby. "
                      "Please turn on the Bluetooth of the target device.")
 
    def connect_target_device(self, target_name, target_address):
        self.find_target_device(target_name=target_name, target_address=target_address)
        if self.find:
            print("Ready to connect")
            sock = bluetooth.BluetoothSocket(bluetooth.Protocols.RFCOMM)
            try:
                # accept_sock = sock.accept(target_address)
                # sock.connect((target_address, 1))
                sock.connect((target_address,3))
                # sock.accept()
                print("Connection successful. Now ready to get the data")
                data_dtr = ""
                # sock.send(b'ATD10086;\r\n')
                imgfile = open("D:\word_cloud.py", "rb")
                sock.sendfile(imgfile)
                
                # while True:
                #     data = sock.recv(1024)
                #     data_dtr += data.decode()
                #     if '\n' in data.decode():
                #         # data_dtr[:-2] 截断"\t\n",只输出数据
                #         print(datetime.datetime.now().strftime("%H:%M:%S")+"->"+data_dtr[:-2])
                #         data_dtr = ""
                
                while True:
                    data = input()
                    if not data:
                        break
                    sock.send(data.encode())
                
            except Exception as e:
                print("connection fail\n", e)
                sock.close()
 
 
if __name__ == '__main__':
    target_name = "vivo X100 Pro"
    target_address = "A8:05:56:45:10:0C"
    # target_name = "Xiaomi 14 Ultra"
    # target_address = "A8:6A:86:B1:31:18"
    BluetoothConnection().connect_target_device(target_name=target_name, target_address=target_address)